/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.network;

import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;

import org.latestbit.smartobjects.IDestroyable;
import org.latestbit.smartobjects.encrypters.IDataEncrypter;

/**
 * Network connection
 * @author abdulla-a
 *
 */
public interface IConnection extends IDestroyable {
	/**
	 * Checks status this connection
	 * @return true - if open, false - if not
	 */
	public boolean isOpen();
	
	/**
	 * Closes this connection
	 */
	public void close();
	
	/**
	 * Gets connection address (represented as URI)
	 * @return Connection address as URI
	 */
	public URI getEndpointURI();

	/**
	 * Sends data to remote side
	 * @param buffer Input byte buffer
	 * @throws IOException
	 */
	public void send(ByteBuffer buffer) throws IOException;

	/**
	 * Adds connection notification listener
	 * @param listener Connection listener
	 */
	public void addConnectionListener(IConnectionListener listener);
	
	/**
	 * Deletes connection notification listener
	 * @param listener Connection listener
	 */
	public void delConnectionListener(IConnectionListener listener);
	
	/**
	 * Adds connection stream listener
	 * @param reader connection stream listener
	 */
	public void addStreamListener(IStreamListener reader);
	
	/**
	 * Deletes connection stream listener
	 * @param reader connection stream listener
	 */	
	public void delStreamListener(IStreamListener reader);
		
	/**
	 * Gets creation information for connection 
	 * @return true - if this connection is created on server, or false for client connector connection
	 */
	public boolean isAcceptorConnection();

	/**
	 * Gets handshake manager for connection
	 * @return Handshake manager
	 */
	public IHandshakeManager getHandshakeManager();
	
	/**
	 * Gets packet former
	 * @return Packet former
	 */
	public IPacketFormer getPacketFormer();
	
	/**
	 * Gets data encrypter for connection
	 * @return Data encrypter
	 */
	public IDataEncrypter getDataEncrypter();
	
	/**
	 * Sets a new data encrypter for this connection
	 * @param encrypter Data encrypter
	 */
	public void setDataEncrypter(IDataEncrypter encrypter);
	
	/**
	 * Gets user credentials associated with this connection by handshake manager.
	 * @return User credentials or null, it not supported by current handshake manager configuration
	 */
	public IUserCredentials getUserCredentials();
	
	/**
	 * Sets the new user credentials for this connection (usually this method uses by handshake managers)
	 * @param credentials New user credentials
	 */
	public void setUserCredentials(IUserCredentials credentials);
	
}
